﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using RBS.Common;
using RBS.Common.JsonEntity;

namespace RBS.Web.API.Areas.Deed.Controllers
{
    /// <summary>
    /// 酒店优惠券
    /// </summary>
    public class HotelDeedController : ApiController
    { 
        /// <summary>
        /// 获取消费券
        /// </summary>
        /// <param name="hotelid"></param>
        /// <param name="type"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonData Get(int hotelid, int type =0, int page = 0, int limit = 5)
        {
            //new RBS.Core.BLL.P_Advertis_Payment().GetListByUser()
            var jsonData = new JsonData();
            try
            {
                var bll = new RBS.Core.BLL.P_Advertis_Payment();
                var strTemp = new StringBuilder();
                string strWhere = " and HotelId=" + hotelid + " and  DeedStatus=0 and  datediff(day,ValidEndTime,GETDATE())<0  ";
                if (type != 0)
                {
                    strWhere += " and DeedType=  " + type + "";
                }
                const string orderby = " ValidEndTime  desc";
                var startIndex = page;

                int totalItems = 0; //要分页的总记录数
                DataTable dt = bll.GetList(limit, startIndex, strWhere, orderby, out totalItems).Tables[0];
                if (dt.Rows.Count == 0)
                {
                    jsonData.Success = true;
                    jsonData.Data = null;
                    jsonData.PageSize = limit;
                    jsonData.Page = startIndex;
                    jsonData.TotalPage = totalItems;
                    jsonData.Error = "暂无数据";
                    return jsonData;
                }
                var list = IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
                jsonData.Success = true;
                jsonData.Data = list;
                jsonData.PageSize = limit;
                jsonData.Page = startIndex;
                jsonData.TotalPage = totalItems;

            }
            catch (Exception ex)
            {

                jsonData.Success = false;
                jsonData.Data = null;
                jsonData.PageSize = limit;
                jsonData.Page = 0;
                jsonData.TotalPage = 0;
                jsonData.Error = ex.Message;
            }
            return jsonData;

        }
        /// <summary>
        /// 赠与用户
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="apid"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public JsonData GetGift(int uid, int apid, int num = 1)
        {
            var jsonData = new JsonData(); 
            if (num > 0)
            {
                var userId = uid;
                for (int i = 0; i < num; i++)
                {
                    AddBatch(apid, userId);
                }
            } 
            jsonData.Success = true; 
            return jsonData;
        }
        /// <summary>
        /// 赠送优惠券
        /// </summary>
        /// <param name="apId"></param>
        /// <param name="userId"></param>
        private void AddBatch(int apId, int userId)
        {
            var model = new RBS.Core.Model.P_Advertis_Batch
            {
                AP_Id = apId,
                BatchTime = DateTime.Now,
                ABStatus = (int)RBSEnums.DeedStatus.Ok,
                BatchNo = Utils.GetGuid()
            };
            var bll = new RBS.Core.BLL.P_Advertis_Batch();
            var abid = bll.Add(model);
            var user = new RBS.Core.Model.P_Advertis_List
            {
                AB_Id = abid,
                U_Id = userId,
                ALStatus = (int)RBSEnums.DeedStatus.Ok,
                ALTime = DateTime.Now
            };
            var userbll = new RBS.Core.BLL.P_Advertis_List();
            userbll.Add(user);
        } 
    }
}
